package com.lyl.syt.api;

import com.lyl.syt.common.result.Result;
import com.lyl.syt.model.hosp.Hospital;
import com.lyl.syt.modules.mongodb.service.HospitalService;
import com.lyl.syt.modules.mp.service.HospitalSetService;
import com.lyl.syt.vo.hosp.HospitalQueryVo;
import com.lyl.syt.vo.order.SignInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController("ApiHospitalController")
@RequestMapping("/api/hosp")
@RequiredArgsConstructor
public class HospitalController extends BaseAPI {
    private final HospitalService hospitalService;
    private final HospitalSetService hospitalSetService;

    @ApiOperation("根据医院获取签名信息")
    @GetMapping("/inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(@PathVariable String hoscode){
        return hospitalSetService.getSignInfoVo(hoscode);
    }

    @ApiOperation("根据医院编号获取预约挂号详情")
    @GetMapping("/findHospDetail/{hoscode}")
    public Result<Map<String,Object>> findHospDetail(@PathVariable String hoscode){
        Map<String,Object> map = hospitalService.findHospDetail(hoscode);
        return Result.ok(map);
    }

    @ApiOperation("查询医院列表")
    @GetMapping("/hospital/findHospList/{page}/{limit}")
    public Result<Page<Hospital>> findHospList(@PathVariable int page,
                                               @PathVariable int limit,
                                               HospitalQueryVo queryVo
                                               ){
        Page<Hospital> hospPage = hospitalService.getHospPage(page, limit, queryVo);
        return Result.ok(hospPage);
    }

    @ApiOperation("根据医院名称查询")
    @GetMapping("/hospital/findByHosname/{hosname}")
    public Result<List<Hospital>> findByHospName(@PathVariable String hosname){
        List<Hospital> list = hospitalService.findByHosname(hosname);
        return Result.ok(list);
    }

    @PostMapping("/hospital/show")
    public Result<Hospital> showHosp(HttpServletRequest request){
        //进行前置处理，进行签名校验
        Map<String, Object> paramMap = prefixHandler(request);
        //根据医院编号进行查询
        return Result.ok(hospitalService.getByHoscode(((String) paramMap.get("hoscode"))));
    }

    /**
     * 上传医院接口
     * @param request 请求体
     * @return boolean
     */
    @PostMapping("/saveHospital")
    public Result<Boolean> saveHosp(HttpServletRequest request){
        Map<String, Object> paramMap = prefixHandler(request);

        //5. 图标传输过程中 + 变成 空格 ，需要转回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }



}
